package lrl.login.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import lrl.core.constant.Constant;
import lrl.nsfw.user.entity.User;
import lrl.nsfw.user.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	@Resource
	private UserService userService;
	
	private User user;
	private String logingResult;
	
	public String loginUI() {
		
		return "loginUI";
	}
	
	public String login() {
		if (user != null) {
			if (StringUtils.isNotBlank(user.getAccount()) && StringUtils.isNotBlank(user.getPassword())) {
				List<User> list = userService.findUserByAccountAndPass(user.getAccount(), user.getPassword());
				if (list != null && list.size() > 0) {
					User user = list.get(0);
					user.setUserRoles(userService.getUserRolesByUserId(user.getId()));
					
					ServletActionContext.getRequest().getSession().setAttribute(Constant.USER, user);
					return "home";
					
				} else {
					logingResult = "账号或密码不正确";
				}
			} else {
				logingResult = "账号或密码不能为空";
			}
		} else {
			logingResult = "请输入账号和密码";
		}
		return loginUI();
	}
	
	public String logout() {
		ServletActionContext.getRequest().getSession().removeAttribute(Constant.USER);
		return loginUI();
	}
	
	public String noPermissionUI() {
		return "noPermissionUI";
	}
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getLogingResult() {
		return logingResult;
	}

	public void setLogingResult(String logingResult) {
		this.logingResult = logingResult;
	}
	
}
